package com.catchu.pattern.creational.迭代器模式.a入门.iterator;

import com.catchu.pattern.creational.迭代器模式.a入门.aggregate.ConcreteAggregate;

public class ConcreteIterator implements Iterator {

    /**
     * 持有具体的聚合对象，并对这个聚合对象进行遍历
     */
    private ConcreteAggregate concreteAggregate;

    /**
     * 遍历聚合对象的索引值，初始为-1
     */
    private int index = -1;

    public ConcreteIterator(ConcreteAggregate concreteAggregate) {
        this.concreteAggregate = concreteAggregate;
    }

    @Override
    public void first() {
        index = 0;
    }

    @Override
    public void next() {
        if(index<concreteAggregate.size()){
            index++;
        }
    }

    @Override
    public boolean isDone() {
        if(index == concreteAggregate.size()){
            return true;
        }
        return false;
    }

    @Override
    public Object currentItem() {
        return concreteAggregate.get(index);
    }
}
